package io.sousou.controller;

import io.sousou.dao.StudentRepository;
import io.sousou.enums.ResultEnum;
import io.sousou.model.Student;
import io.sousou.service.StudentService;
import io.sousou.utils.R;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/student")
public class StudentController {
	@Autowired
	private StudentRepository studentRepository;
	@Autowired
	private StudentService studentService;
	@GetMapping(value="/gets")
	public R getss(){
		Student s= studentService.getOne(1);
		return R.ok(s);
	}
	@PostMapping(value="save")
	public R save(@Valid Student student,BindingResult bindingResult){
		
		if (bindingResult.hasErrors()) {
            return R.error(ResultEnum.notBlack(bindingResult.getFieldError().getDefaultMessage()));
        }
		
		studentRepository.save(student);
		return R.ok();
	}
	@GetMapping(value="/getall")
	public List<Student> getall(){
		return studentService.getAll();
	}
	@GetMapping(value="/index")
	public ModelAndView index(){
		return new ModelAndView("index");
	}
}
